package com.zmkj.service.Imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zmkj.dao.SysRoleDao;
import com.zmkj.dao.SysUserDao;
import com.zmkj.domain.SysRole;
import com.zmkj.domain.SysUser;
import com.zmkj.service.SysUserService;
import com.zmkj.utils.Code;
import com.zmkj.utils.Rest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;



@Service
public class SysUserServiceImp implements SysUserService {

    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private SysRoleDao sysRoleDao;

    @Override
    public Rest checkUser(SysUser sysUser) {
        LambdaQueryWrapper<SysUser> lamwarrper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<SysUser> eqs = lamwarrper.eq(SysUser::getUsername, sysUser.getUsername());
        SysUser sysUser1 = sysUserDao.selectOne(lamwarrper);
        if(Objects.isNull(sysUser1)){
            return new Rest(Code.rc400.getCode(),"用户名不存在");
        }
        eqs.eq(SysUser::getPassword,sysUser.getPassword());
        SysUser sysUser2 = sysUserDao.selectOne(lamwarrper);
        if(Objects.isNull(sysUser2)){
            return new Rest(Code.rc400.getCode(),"用户名密码不正确");
        }
        eqs.eq(SysUser::getRoleId,sysUser.getRoleId());
        SysUser sysUser3 = sysUserDao.selectOne(lamwarrper);
        if(Objects.isNull(sysUser3)){
            return new Rest(Code.rc400.getCode(),"用户名权限不符合");
        }
        sysUser3.setPassword("**********");
        SysRole roledata = sysRoleDao.getUserRole(sysUser.getRoleId());
        sysUser3.setSysRole(roledata);
        sysUser3.setRoleId(sysUser.getRoleId());
        return new Rest(Code.rc200.getCode(),sysUser3,"登录成功");
    }

    @Override
    public Rest GetUserData(Integer userid) {
        LambdaQueryWrapper<SysUser> userwarpper = new LambdaQueryWrapper<>();
        userwarpper.eq(SysUser::getId,userid);
        SysUser sysUserData = sysUserDao.selectOne(userwarpper);
        return new Rest(Code.rc200.getCode(),sysUserData,"用户数据获取成功");
    }

    @Override
    public Rest getAllUserData(Integer userId) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.ne(SysUser::getId,userId);
        List<SysUser> userlist = sysUserDao.selectList(wrapper);
//        ArrayList<SysUser> sysUserdataList = new ArrayList<>();
        for(SysUser user : userlist){
            SysUser sysUser = new SysUser();
            if(user.getRoleId() == 1){
                user.setRolename("学生");
            } else if (user.getRoleId() == 2) {
                user.setRolename("老师");
            } else if (user.getRoleId() == 3) {
                user.setRolename("助教");
            } else if (user.getRoleId() == 4) {
                user.setRolename("管理员");
            }else {
                user.setRolename("权限查询失败");
            }
        }
        return new Rest(Code.rc200.getCode(), userlist,Code.rc200.getMsg());
    }

}
